package com.topscomm.mis.cbb.pm.controller;

import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.cbo.service.ICboApprovalNodeInsService;
import com.topscomm.cbo.service.ICboApprovalNodeService;
import com.topscomm.mis.cbb.pm.api.ITaskService;
import com.topscomm.mis.cbb.pm.base.pojo.CbbPmTaskEntity;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskBackRecordService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskEditDocService;
import com.topscomm.mis.cbb.pm.base.service.ICbbPmTaskService;
import com.topscomm.mis.cbb.pm.controller.auto.CbbPmTaskControllerAuto;
import com.topscomm.mis.cbb.pm.util.PmSystemConst;
import com.topscomm.pub.util.BaseUtil;
import com.topscomm.pub.util.ConvertUtil;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.pub.util.StringUtil;
import com.topscomm.pub.vo.Page;
import com.topscomm.pub.vo.Query;
import com.topscomm.pub.vo.ResponseResult;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Validated
@RestController
@RequestMapping("cbb/pmTask")
public class CbbPmTaskController extends CbbPmTaskControllerAuto {
    @Autowired
    private ITaskService taskService;
    @Autowired
    private ICboApprovalNodeInsService cboApprovalNodeInsService;
    @Autowired
    private ICboApprovalNodeService cboApprovalNodeService;
    @Autowired
    private ICbbPmTaskBackRecordService cbbPmTaskBackRecordService;
    @Autowired
    private ICbbPmTaskService cbbPmTaskService;
    @Autowired
    private ICbbPmTaskEditDocService cbbPmTaskEditDocService;

    private String spellListSql(Map<String, Object> paraMap) {
        StringBufferProxy sql = new StringBufferProxy();
        sql.appendSingle(" 1=1 ");
        if (paraMap.containsKey("qry_wbscode")) {
            String value = paraMap.get("qry_wbscode").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and wbscode = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_responsibleusercode")) {
            String value = paraMap.get("qry_responsibleusercode").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and responsibleusercode = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_projectuucode")) {
            String value = paraMap.get("qry_projectuucode").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and projectuucode = '{0}' ", value);
            }
        }
        if (paraMap.containsKey("qry_code")) {
            String value = paraMap.get("qry_code").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and code like '%{0}%' ", value);
            }
        }
        if (paraMap.containsKey("qry_name")) {
            String value = paraMap.get("qry_name").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and name like '%{0}%' ", value);
            }
        }
        if (paraMap.containsKey("qry_deliverystatus")) {
            String value = paraMap.get("qry_deliverystatus").toString();
            if (!StringUtil.isEmpty(value)) {
                sql.appendSingle(" and deliverystatus = {0} ", value);
            }
        }
        return sql.toString();
    }
    /***
     * @description: 获取甘特图数据
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult
     * @author: wangxinhua
     * @date: 2023/6/14 8:53
     * @modify:
     */
    @GetMapping(value = "/ganttInfo")
    public ResponseResult ganttInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> resultMap=taskService.getGanttInfo(paraMap);
        return ResponseResult.ok(resultMap);
    }


    /**
     * @description: 新增
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/14 10:35
     * @modify:
     */
    @PostMapping(value = "/insert")
    public ResponseResult<Object> insert(@RequestBody(required = false) Map<String, Object> paraMap) {
        Map<String, Object> resMap = taskService.insert(paraMap);
        return ResponseResult.ok(resMap);
    }


    /**
     * @description: 修改
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/14 10:35
     * @modify:
     */
    @PutMapping(value = "/update")
    public ResponseResult<Object> update(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.update(paraMap);
        return ResponseResult.ok("修改成功");
    }



    /**
     * @description: 删除
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/14 10:35
     * @modify:
     */
    @DeleteMapping(value = "/delete")
    public ResponseResult<Object> delete(@RequestBody(required = false) Map<String, Object> paraMap) {
        BaseUtil.decodeSecureMap(paraMap);
        taskService.delete(paraMap);
        return ResponseResult.ok("删除成功");
    }


    /**
     * @description: 加载数据
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/14 10:35
     * @modify:
     */
    @GetMapping(value = "/load")
    public ResponseResult<Object> load(@RequestParam(required = false) Map<String, Object> paraMap) {
        BaseUtil.decodeSecureMap(paraMap);
        Map<String, Object> dataMap = taskService.load(paraMap);
        return ResponseResult.ok(dataMap);
    }

    /**
     * @description: 查询列表
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:47
     * @modify:
     */
    @GetMapping(value = "/list")
    public ResponseResult<Object> list(@RequestParam(required = false) Map<String, Object> paraMap) {
        BaseUtil.decodeSecureMap(paraMap);
        String sqlWhere = this.spellListSql(paraMap);
        Page page = taskService.list(paraMap, sqlWhere);
        return ResponseResult.ok(page);
    }

    /**
     * @description: 下发单个任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/issueTask")
    public ResponseResult<Object> issueTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.issueTask(paraMap);
        return ResponseResult.ok("下发成功");
    }

    /**
     * @description: 下发WBS所有任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/deliveryWbs")
    public ResponseResult<Object> deliveryWbs(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.deliveryWbs(paraMap);
        return ResponseResult.ok("下发成功");
    }

    /**
     * @description: 开始任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/startTask")
    public ResponseResult<Object> startTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.startTask(paraMap);
        return ResponseResult.ok("开始任务成功");
    }

    /**
     * @description: 完成任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/completeTask")
    public ResponseResult<Object> completeTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.completeTask(paraMap);
        return ResponseResult.ok("完成任务成功");
    }

    /**
     * @description: 退回任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/backTask")
    public ResponseResult<Object> backTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.backTask(paraMap);
        return ResponseResult.ok("完成任务成功");
    }

    /**
     * @description: 退回人工任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/backLabourTask")
    public ResponseResult<Object> backLabourTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.backLabourTask(paraMap);
        return ResponseResult.ok("退回任务成功");
    }

    /**
     * @description: 退回人工任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/confirmBackLabourTask")
    public ResponseResult<Object> confirmBackLabourTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.confirmBackLabourTask(paraMap);
        return ResponseResult.ok("操作成功");
    }

    /**
     * @description: 关闭人工任务
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @PostMapping(value = "/closeEditLabourTask")
    public ResponseResult<Object> closeEditLabourTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.closeEditLabourTask(paraMap);
        return ResponseResult.ok("操作成功");
    }

    /**
     * @description: 退回任务记录详情
     * @param paraMap
     * @return com.topscomm.pub.vo.ResponseResult<java.lang.Object>
     * @author: wangxinhua
     * @date: 2023/6/20 14:46
     * @modify:
     */
    @GetMapping(value = "/backRecordInfo")
    public ResponseResult<Object> backRecordInfo(@RequestParam(required = false) Map<String, Object> paraMap) {
        BaseUtil.decodeSecureMap(paraMap);
        Map<String, Object> dataMap = taskService.backRecordInfo(paraMap);
        return ResponseResult.ok(dataMap);
    }

    /**
     * @description: 批量修改
     * @param paraMap
     * @return
     * @author: hudeyong
     * @date: 2023/6/25
     * @modify:
     */
    @PutMapping(value = "/saveBatchTask")
    public ResponseResult<Object> saveBatchTask(@RequestBody(required = false) Map<String, Object> paraMap) {
        taskService.saveBatchTask(paraMap);
        return ResponseResult.ok("保存成功");
    }

    @GetMapping("/queryTaskNodeInfo")
    public ResponseResult<Object> queryTaskNodeInfo(@RequestParam Map<String, Object> paraMap) {
        paraMap = BaseUtil.decodeSecureMap(paraMap);
        List<Map<String, Object>> list = cboApprovalNodeService.queryMapByWhere("id=" + ConvertUtil.convertToString(paraMap.get("nodeid")));
        if (list != null && list.size() > 0) {
            CbbPmTaskEntity taskEntity = cbbPmTaskService.queryById(ConvertUtil.convertToString(paraMap.get("taskid")));
            List<Map<String, Object>> recordInfo = cbbPmTaskBackRecordService.queryMapByWhere(" code='"+taskEntity.getCode()+"' and handlestate="+TapSystemConst.TapApprovalFlowStateEnum.Approveing);
            Map<String, Object> resMap = new HashMap<>();
            resMap.put("nodeModel", list.get(0));
            resMap.put("recordInfo", recordInfo.size() > 0 ? recordInfo.get(0) : null);
            // 是否有子级特殊任务填写的数据
            List<Map<String, Object>> editDocInfo = cbbPmTaskEditDocService.queryMapByWhere(" parentcode='"+taskEntity.getCode()+"' and round='"+taskEntity.getRound()+"'");
            resMap.put("editDocInfo", editDocInfo.size() > 0 ? editDocInfo.get(0) : null);
            return ResponseResult.ok(resMap);
        } else {
            return ResponseResult.error("没有找到对应的节点数据！");
        }

    }

    @GetMapping(value = "/queryInitData")
    public ResponseResult<Object> queryInitData(@RequestParam(required = false) Map<String, Object> paraMap) {
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("enabled", "1");
        //赋值id,code
        long[] arrayId = PrimaryKeyUtil.GeneIDsFromCache(CbbPmTaskEntity.tableName, 1);
        dataMap.put("id", arrayId[0]);
        dataMap.put("code", PmSystemConst.CodePrefix.TASK + ConvertUtil.convertToString(arrayId[0]));
        return ResponseResult.ok(dataMap);
    }
}
